package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.MmpiDetail;
import org.dtrd.modules.evaluation.entity.bean.MmpiListInfo;
import org.dtrd.modules.evaluation.entity.bean.MmpiListRequest;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdMmpi;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdMmpiMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdMmpiService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.stream.Collectors;


/**
 * <p>
 * 明苏尼达多相个性评估 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-15
 */
@DS("multi-datasource1")
@Service
public class DtrdEntEvaluationRdMmpiServiceImpl extends ServiceImpl<DtrdEntEvaluationRdMmpiMapper, DtrdEntEvaluationRdMmpi> implements IDtrdEntEvaluationRdMmpiService {

    private DtrdEntEvaluationRdMmpiMapper dtrdEntEvaluationRdMmpiMapper;

    @Override
    public List<MmpiListInfo> getMmpiList(MmpiListRequest request) {
        LambdaQueryWrapper<DtrdEntEvaluationRdMmpi> queryWrapper = baseQueryWrapper();
        Integer patientId = request.getPatientId();
        if (patientId != null) {
            queryWrapper.eq(DtrdEntEvaluationRdMmpi::getPatientId, patientId);
        }
        List<DtrdEntEvaluationRdMmpi> poList = list(queryWrapper);
        List<MmpiListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(MmpiListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public MmpiDetail getLatestMmpi(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdMmpi> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdMmpi.class)
                .eq(DtrdEntEvaluationRdMmpi::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdMmpi::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdMmpi dtrdEntEvaluationRdMmpi = getOne(queryWrapper);
        return Optional.ofNullable(dtrdEntEvaluationRdMmpi)
                        .map(po -> new MmpiDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public MmpiDetail saveOrUpdateMmpi(MmpiDetail dto) {
        int tmpScore = 0;
        int gender = dto.getGender();
        int fq = 0;
        int fl = 0;
        int ff = 0;
        int fk = 0;
        int fhs = 0;
        int fd = 0;
        int fhy = 0;
        int fpd = 0;
        int fmfm = 0;
        int fmff = 0;
        int fpa = 0;
        int fpt = 0;
        int fsc = 0;
        int fma = 0;
        int fsi = 0;
        int fmas = 0;
        int fdy = 0;
        int fdo = 0;
        int fre = 0;
        int fcn = 0;
        StringJoiner joiner = new StringJoiner(",");
        List<Integer> input = dto.getInput();
        for (int i = 0; i < input.size(); i++) {
            tmpScore = input.get(i);
            joiner.add(tmpScore + "");
            if (tmpScore == 1)
                fq++;
            else if (tmpScore == 0) {
                if (14 == i + 1 || 27 == i + 1 || 31 == i + 1 || 34 == i + 1 || 35 == i + 1 || 40 == i + 1 || 42 == i + 1 || 48 == i + 1 || 49 == i + 1
                        || 50 == i + 1 || 53 == i + 1 || 56 == i + 1 || 66 == i + 1 || 85 == i + 1 || 121 == i + 1 || 123 == i + 1 || 139 == i + 1
                        || 146 == i + 1 || 151 == i + 1 || 156 == i + 1 || 168 == i + 1 || 184 == i + 1 || 197 == i + 1 || 200 == i + 1 || 202 == i + 1
                        || 205 == i + 1 || 206 == i + 1 || 209 == i + 1 || 210 == i + 1 || 211 == i + 1 || 215 == i + 1 || 218 == i + 1 || 227 == i + 1
                        || 245 == i + 1 || 246 == i + 1 || 247 == i + 1 || 252 == i + 1 || 256 == i + 1 || 269 == i + 1 || 275 == i + 1 || 286 == i + 1
                        || 288 == i + 1 || 291 == i + 1 || 293 == i + 1)
                    ff++;
                if (96 == i + 1)
                    fk++;
                if (10 == i + 1 || 23 == i + 1 || 32 == i + 1 || 43 == i + 1 || 44 == i + 1 || 47 == i + 1 || 76 == i + 1 || 114 == i + 1 || 179 == i + 1
                        || 186 == i + 1 || 189 == i + 1 || 238 == i + 1 || 253 == i + 1)
                    fhy++;
                if (5 == i + 1 || 32 == i + 1 || 41 == i + 1 || 43 == i + 1 || 52 == i + 1 || 67 == i + 1 || 86 == i + 1 || 104 == i + 1 || 130 == i + 1
                        || 138 == i + 1 || 142 == i + 1 || 158 == i + 1 || 159 == i + 1 || 182 == i + 1 || 189 == i + 1 || 193 == i + 1 || 236 == i + 1
                        || 259 == i + 1 || 288 == i + 1 || 290 == i + 1)
                    fd++;
                if (23 == i + 1 || 29 == i + 1 || 43 == i + 1 || 62 == i + 1 || 72 == i + 1 || 108 == i + 1 || 114 == i + 1 || 125 == i + 1 || 161 == i + 1
                        || 189 == i + 1 || 273 == i + 1)
                    fhs++;
                if (16 == i + 1 || 21 == i + 1 || 24 == i + 1 || 32 == i + 1 || 33 == i + 1 || 35 == i + 1 || 38 == i + 1 || 42 == i + 1 || 61 == i + 1
                        || 67 == i + 1 || 84 == i + 1 || 94 == i + 1 || 102 == i + 1 || 106 == i + 1 || 110 == i + 1 || 118 == i + 1 || 127 == i + 1
                        || 215 == i + 1 || 216 == i + 1 || 224 == i + 1 || 239 == i + 1 || 244 == i + 1 || 245 == i + 1 || 284 == i + 1)
                    fpd++;
                if (1 == i + 1 || 25 == i + 1 || 69 == i + 1 || 70 == i + 1 || 74 == i + 1 || 77 == i + 1 || 78 == i + 1 || 87 == i + 1 || 92 == i + 1
                        || 126 == i + 1 || 132 == i + 1 || 134 == i + 1 || 140 == i + 1 || 149 == i + 1 || 179 == i + 1 || 187 == i + 1 || 203 == i + 1
                        || 204 == i + 1 || 217 == i + 1 || 226 == i + 1 || 231 == i + 1 || 239 == i + 1 || 261 == i + 1 || 278 == i + 1 || 282 == i + 1
                        || 295 == i + 1 || 297 == i + 1 || 299 == i + 1)
                    fmfm++;
                if (4 == i + 1 || 25 == i + 1 || 70 == i + 1 || 74 == i + 1 || 77 == i + 1 || 78 == i + 1 || 87 == i + 1 || 92 == i + 1 || 126 == i + 1
                        || 132 == i + 1 || 133 == i + 1 || 134 == i + 1 || 140 == i + 1 || 149 == i + 1 || 187 == i + 1 || 203 == i + 1 || 204 == i + 1
                        || 217 == i + 1 || 226 == i + 1 || 239 == i + 1 || 261 == i + 1 || 278 == i + 1 || 282 == i + 1 || 295 == i + 1 || 299 == i + 1)
                    fmff++;
                if (16 == i + 1 || 24 == i + 1 || 27 == i + 1 || 35 == i + 1 || 110 == i + 1 || 121 == i + 1 || 123 == i + 1 || 127 == i + 1 || 151 == i + 1
                        || 157 == i + 1 || 158 == i + 1 || 202 == i + 1 || 275 == i + 1 || 284 == i + 1 || 291 == i + 1 || 293 == i + 1 || 299 == i + 1
                        || 305 == i + 1 || 314 == i + 1 || 317 == i + 1 || 326 == i + 1 || 338 == i + 1 || 341 == i + 1 || 364 == i + 1 || 365 == i + 1)
                    fpa++;
                if (10 == i + 1 || 15 == i + 1 || 22 == i + 1 || 32 == i + 1 || 41 == i + 1 || 67 == i + 1 || 76 == i + 1 || 86 == i + 1 || 94 == i + 1
                        || 102 == i + 1 || 106 == i + 1 || 142 == i + 1 || 159 == i + 1 || 182 == i + 1 || 189 == i + 1 || 217 == i + 1 || 238 == i + 1
                        || 266 == i + 1 || 301 == i + 1 || 304 == i + 1 || 321 == i + 1 || 336 == i + 1 || 337 == i + 1 || 340 == i + 1 || 342 == i + 1
                        || 343 == i + 1 || 344 == i + 1 || 346 == i + 1 || 349 == i + 1 || 351 == i + 1 || 352 == i + 1 || 356 == i + 1 || 357 == i + 1
                        || 358 == i + 1 || 359 == i + 1 || 360 == i + 1 || 361 == i + 1 || 362 == i + 1 || 366 == i + 1)
                    fpt++;
                if (15 == i + 1 || 22 == i + 1 || 40 == i + 1 || 41 == i + 1 || 47 == i + 1 || 52 == i + 1 || 76 == i + 1 || 97 == i + 1 || 104 == i + 1
                        || 102 == i + 1 || 156 == i + 1 || 157 == i + 1 || 159 == i + 1 || 168 == i + 1 || 179 == i + 1 || 182 == i + 1 || 194 == i + 1
                        || 202 == i + 1 || 210 == i + 1 || 212 == i + 1 || 238 == i + 1 || 241 == i + 1 || 251 == i + 1 || 259 == i + 1 || 266 == i + 1
                        || 273 == i + 1 || 282 == i + 1 || 291 == i + 1 || 297 == i + 1 || 301 == i + 1 || 303 == i + 1 || 307 == i + 1 || 308 == i + 1
                        || 311 == i + 1 || 312 == i + 1 || 315 == i + 1 || 320 == i + 1 || 323 == i + 1 || 324 == i + 1 || 325 == i + 1 || 328 == i + 1
                        || 331 == i + 1 || 332 == i + 1 || 333 == i + 1 || 334 == i + 1 || 335 == i + 1 || 339 == i + 1 || 341 == i + 1 || 345 == i + 1
                        || 349 == i + 1 || 350 == i + 1 || 352 == i + 1 || 354 == i + 1 || 355 == i + 1 || 356 == i + 1 || 360 == i + 1 || 363 == i + 1
                        || 364 == i + 1 || 366 == i + 1)
                    fsc++;
                if (11 == i + 1 || 13 == i + 1 || 21 == i + 1 || 22 == i + 1 || 59 == i + 1 || 64 == i + 1 || 73 == i + 1 || 97 == i + 1 || 100 == i + 1
                        || 109 == i + 1 || 127 == i + 1 || 134 == i + 1 || 143 == i + 1 || 156 == i + 1 || 157 == i + 1 || 167 == i + 1 || 181 == i + 1
                        || 194 == i + 1 || 212 == i + 1 || 222 == i + 1 || 226 == i + 1 || 228 == i + 1 || 232 == i + 1 || 233 == i + 1 || 238 == i + 1
                        || 240 == i + 1 || 250 == i + 1 || 251 == i + 1 || 262 == i + 1 || 266 == i + 1 || 268 == i + 1 || 271 == i + 1 || 277 == i + 1
                        || 279 == i + 1 || 298 == i + 1)
                    fma++;
                if (32 == i + 1 || 67 == i + 1 || 82 == i + 1 || 111 == i + 1 || 117 == i + 1 || 124 == i + 1 || 138 == i + 1 || 147 == i + 1
                        || 171 == i + 1 || 172 == i + 1 || 180 == i + 1 || 201 == i + 1 || 236 == i + 1 || 267 == i + 1 || 278 == i + 1 || 292 == i + 1
                        || 304 == i + 1 || 316 == i + 1 || 321 == i + 1 || 332 == i + 1 || 336 == i + 1 || 342 == i + 1 || 257 == i + 1 || 369 == i + 1
                        || 370 == i + 1 || 373 == i + 1 || 376 == i + 1 || 378 == i + 1 || 379 == i + 1 || 385 == i + 1 || 389 == i + 1 || 393 == i + 1
                        || 398 == i + 1 || 399 == i + 1)
                    fsi++;
                if (13 == i + 1 || 14 == i + 1 || 23 == i + 1 || 31 == i + 1 || 32 == i + 1 || 43 == i + 1 || 67 == i + 1 || 86 == i + 1 || 125 == i + 1
                        || 142 == i + 1 || 158 == i + 1 || 186 == i + 1 || 191 == i + 1 || 217 == i + 1 || 238 == i + 1 || 241 == i + 1 || 263 == i + 1
                        || 301 == i + 1 || 317 == i + 1 || 321 == i + 1 || 322 == i + 1 || 335 == i + 1 || 337 == i + 1 || 340 == i + 1 || 352 == i + 1
                        || 361 == i + 1 || 397 == i + 1 || 418 == i + 1 || 424 == i + 1 || 431 == i + 1 || 439 == i + 1 || 442 == i + 1 || 499 == i + 1
                        || 506 == i + 1 || 530 == i + 1 || 449 == i + 1 || 555 == i + 1)
                    fmas++;
                if (19 == i + 1 || 21 == i + 1 || 24 == i + 1 || 41 == i + 1 || 63 == i + 1 || 67 == i + 1 || 70 == i + 1 || 82 == i + 1 || 86 == i + 1
                        || 98 == i + 1 || 100 == i + 1 || 138 == i + 1 || 141 == i + 1 || 158 == i + 1 || 165 == i + 1 || 180 == i + 1 || 189 == i + 1
                        || 201 == i + 1 || 212 == i + 1 || 236 == i + 1 || 239 == i + 1 || 259 == i + 1 || 267 == i + 1 || 304 == i + 1 || 305 == i + 1
                        || 321 == i + 1 || 337 == i + 1 || 338 == i + 1 || 343 == i + 1 || 357 == i + 1 || 361 == i + 1 || 362 == i + 1 || 375 == i + 1
                        || 382 == i + 1 || 383 == i + 1 || 390 == i + 1 || 394 == i + 1 || 397 == i + 1 || 398 == i + 1 || 408 == i + 1 || 443 == i + 1
                        || 487 == i + 1 || 488 == i + 1 || 489 == i + 1 || 509 == i + 1 || 531 == i + 1 || 549 == i + 1 || 554 == i + 1 || 564 == i + 1)
                    fdy++;
                if (64 == i + 1 || 229 == i + 1 || 255 == i + 1 || 270 == i + 1 || 368 == i + 1 || 432 == i + 1 || 523 == i + 1)
                    fdo++;
                if (58 == i + 1 || 111 == i + 1 || 173 == i + 1 || 221 == i + 1 || 294 == i + 1 || 412 == i + 1 || 501 == i + 1 || 552 == i + 1)
                    fre++;
                if (6 == i + 1 || 20 == i + 1 || 30 == i + 1 || 56 == i + 1 || 67 == i + 1 || 105 == i + 1 || 116 == i + 1 || 134 == i + 1 || 145 == i + 1
                        || 162 == i + 1 || 169 == i + 1 || 181 == i + 1 || 225 == i + 1 || 236 == i + 1 || 238 == i + 1 || 285 == i + 1 || 296 == i + 1
                        || 319 == i + 1 || 337 == i + 1 || 382 == i + 1 || 411 == i + 1 || 418 == i + 1 || 436 == i + 1 || 446 == i + 1 || 447 == i + 1
                        || 460 == i + 1 || 529 == i + 1 || 555 == i + 1)
                    fcn++;
            } else {
                if (15 == i + 1 || 30 == i + 1 || 45 == i + 1 || 60 == i + 1 || 75 == i + 1 || 90 == i + 1 || 105 == i + 1 || 120 == i + 1 || 135 == i + 1
                        || 150 == i + 1 || 165 == i + 1 || 195 == i + 1 || 225 == i + 1 | 255 == i + 1 || 285 == i + 1) {
                    fl++;
                }
                if (17 == i + 1 || 20 == i + 1 || 54 == i + 1 || 65 == i + 1 || 75 == i + 1 || 83 == i + 1 || 112 == i + 1 || 113 == i + 1 || 115 == i + 1
                        || 164 == i + 1 || 169 == i + 1 || 177 == i + 1 || 185 == i + 1 || 196 == i + 1 || 199 == i + 1 || 220 == i + 1 || 257 == i + 1
                        || 258 == i + 1 || 272 == i + 1 || 276 == i + 1)
                    ff++;
                if (30 == i + 1 || 39 == i + 1 || 71 == i + 1 || 89 == i + 1 || 124 == i + 1 || 129 == i + 1 || 134 == i + 1 || 138 == i + 1 || 142 == i + 1
                        || 148 == i + 1 || 160 == i + 1 || 170 == i + 1 || 171 == i + 1 || 180 == i + 1 || 183 == i + 1 || 217 == i + 1 || 234 == i + 1
                        || 267 == i + 1 || 272 == i + 1 || 296 == i + 1 || 316 == i + 1 || 322 == i + 1 || 368 == i + 1 || 370 == i + 1 || 372 == i + 1
                        || 373 == i + 1 || 375 == i + 1 || 386 == i + 1 || 394 == i + 1)
                    fk++;
                if (2 == i + 1 || 3 == i + 1 || 6 == i + 1 || 7 == i + 1 || 8 == i + 1 || 9 == i + 1 || 12 == i + 1 || 26 == i + 1 || 30 == i + 1
                        || 51 == i + 1 || 55 == i + 1 || 71 == i + 1 || 89 == i + 1 || 93 == i + 1 || 103 == i + 1 || 107 == i + 1 || 109 == i + 1
                        || 124 == i + 1 || 128 == i + 1 || 129 == i + 1 || 136 == i + 1 || 137 == i + 1 || 141 == i + 1 || 147 == i + 1 || 153 == i + 1
                        || 160 == i + 1 || 162 == i + 1 || 163 == i + 1 || 170 == i + 1 || 172 == i + 1 || 174 == i + 1 || 175 == i + 1 || 180 == i + 1
                        || 188 == i + 1 || 190 == i + 1 || 192 == i + 1 || 201 == i + 1 || 213 == i + 1 || 230 == i + 1 || 234 == i + 1 || 243 == i + 1
                        || 265 == i + 1 || 267 == i + 1 || 274 == i + 1 || 279 == i + 1 || 289 == i + 1 || 292 == i + 1)
                    fhy++;
                if (2 == i + 1 || 8 == i + 1 || 9 == i + 1 || 18 == i + 1 || 30 == i + 1 || 36 == i + 1 || 39 == i + 1 || 46 == i + 1 || 51 == i + 1
                        || 57 == i + 1 || 58 == i + 1 || 64 == i + 1 || 80 == i + 1 || 88 == i + 1 || 89 == i + 1 || 95 == i + 1 || 98 == i + 1
                        || 107 == i + 1 || 122 == i + 1 || 131 == i + 1 || 145 == i + 1 || 152 == i + 1 || 153 == i + 1 || 154 == i + 1 || 155 == i + 1
                        || 160 == i + 1 || 178 == i + 1 || 191 == i + 1 || 207 == i + 1 || 208 == i + 1 || 233 == i + 1 || 241 == i + 1 || 242 == i + 1
                        || 248 == i + 1 || 263 == i + 1 || 270 == i + 1 || 271 == i + 1 || 272 == i + 1 || 285 == i + 1 || 296 == i + 1)
                    fd++;
                if (2 == i + 1 || 3 == i + 1 || 7 == i + 1 || 9 == i + 1 || 18 == i + 1 || 51 == i + 1 || 55 == i + 1 || 63 == i + 1 || 68 == i + 1
                        || 103 == i + 1 || 130 == i + 1 || 153 == i + 1 || 155 == i + 1 || 163 == i + 1 || 175 == i + 1 || 188 == i + 1 || 190 == i + 1
                        || 192 == i + 1 || 230 == i + 1 || 243 == i + 1 || 274 == i + 1 || 281 == i + 1)
                    fhs++;
                if (8 == i + 1 || 20 == i + 1 || 37 == i + 1 || 82 == i + 1 || 91 == i + 1 || 96 == i + 1 || 107 == i + 1 || 134 == i + 1 || 137 == i + 1
                        || 141 == i + 1 || 155 == i + 1 || 170 == i + 1 || 171 == i + 1 || 173 == i + 1 || 180 == i + 1 || 183 == i + 1 || 201 == i + 1
                        || 231 == i + 1 || 235 == i + 1 || 237 == i + 1 || 248 == i + 1 || 267 == i + 1 || 286 == i + 1 || 289 == i + 1 || 294 == i + 1
                        || 296 == i + 1)
                    fpd++;
                if (1 == i + 1 || 19 == i + 1 || 26 == i + 1 || 28 == i + 1 || 79 == i + 1 || 80 == i + 1 || 81 == i + 1 || 89 == i + 1 || 99 == i + 1
                        || 112 == i + 1 || 115 == i + 1 || 116 == i + 1 || 117 == i + 1 || 120 == i + 1 || 133 == i + 1 || 144 == i + 1 || 176 == i + 1
                        || 198 == i + 1 || 213 == i + 1 || 214 == i + 1 || 219 == i + 1 || 221 == i + 1 || 223 == i + 1 || 229 == i + 1 || 249 == i + 1
                        || 254 == i + 1 || 260 == i + 1 || 262 == i + 1 || 264 == i + 1 || 280 == i + 1 || 283 == i + 1 || 300 == i + 1)
                    fmfm++;
                if (1 == i + 1 || 19 == i + 1 || 26 == i + 1 || 28 == i + 1 || 69 == i + 1 || 79 == i + 1 || 80 == i + 1 || 81 == i + 1 || 89 == i + 1
                        || 99 == i + 1 || 112 == i + 1 || 115 == i + 1 || 116 == i + 1 || 117 == i + 1 || 120 == i + 1 || 144 == i + 1 || 176 == i + 1
                        || 179 == i + 1 || 198 == i + 1 || 213 == i + 1 || 214 == i + 1 || 219 == i + 1 || 221 == i + 1 || 223 == i + 1 || 229 == i + 1
                        || 231 == i + 1 || 249 == i + 1 || 254 == i + 1 || 260 == i + 1 || 262 == i + 1 || 264 == i + 1 || 280 == i + 1 || 283 == i + 1
                        || 297 == i + 1 || 300 == i + 1)
                    fmff++;
                if (93 == i + 1 || 107 == i + 1 || 109 == i + 1 || 111 == i + 1 || 117 == i + 1 || 124 == i + 1 || 268 == i + 1 || 281 == i + 1
                        || 294 == i + 1 || 313 == i + 1 || 316 == i + 1 || 319 == i + 1 || 327 == i + 1 || 347 == i + 1 || 348 == i + 1)
                    fpa++;
                if (3 == i + 1 || 8 == i + 1 || 36 == i + 1 || 122 == i + 1 || 152 == i + 1 || 164 == i + 1 || 178 == i + 1 || 329 == i + 1 || 353 == i + 1)
                    fpt++;
                if (17 == i + 1 || 65 == i + 1 || 103 == i + 1 || 119 == i + 1 || 177 == i + 1 || 178 == i + 1 || 187 == i + 1 || 192 == i + 1
                        || 196 == i + 1 || 220 == i + 1 || 276 == i + 1 || 281 == i + 1 || 302 == i + 1 || 306 == i + 1 || 309 == i + 1 || 310 == i + 1
                        || 318 == i + 1 || 322 == i + 1 || 330 == i + 1)
                    fsc++;
                if (101 == i + 1 || 105 == i + 1 || 111 == i + 1 || 119 == i + 1 || 120 == i + 1 || 148 == i + 1 || 166 == i + 1 || 171 == i + 1
                        || 180 == i + 1 || 267 == i + 1 || 289 == i + 1)
                    fma++;
                if (25 == i + 1 || 33 == i + 1 || 57 == i + 1 || 91 == i + 1 || 99 == i + 1 || 119 == i + 1 || 126 == i + 1 || 143 == i + 1 || 193 == i + 1
                        || 208 == i + 1 || 229 == i + 1 || 231 == i + 1 || 254 == i + 1 || 262 == i + 1 || 281 == i + 1 || 296 == i + 1 || 309 == i + 1
                        || 353 == i + 1 || 359 == i + 1 || 367 == i + 1 || 371 == i + 1 || 374 == i + 1 || 377 == i + 1 || 380 == i + 1 || 381 == i + 1
                        || 382 == i + 1 || 383 == i + 1 || 384 == i + 1 || 387 == i + 1 || 388 == i + 1 || 390 == i + 1 || 391 == i + 1 || 392 == i + 1
                        || 395 == i + 1 || 396 == i + 1 || 397 == i + 1)
                    fsi++;
                if (7 == i + 1 || 18 == i + 1 || 107 == i + 1 || 163 == i + 1 || 190 == i + 1 || 230 == i + 1 || 242 == i + 1 || 264 == i + 1
                        || 287 == i + 1 || 371 == i + 1 || 407 == i + 1 || 523 == i + 1 || 528 == i + 1)
                    fmas++;
                if (9 == i + 1 || 79 == i + 1 || 107 == i + 1 || 163 == i + 1 || 170 == i + 1 || 193 == i + 1 || 264 == i + 1 || 369 == i + 1)
                    fdy++;
                if (32 == i + 1 || 61 == i + 1 || 82 == i + 1 || 86 == i + 1 || 94 == i + 1 || 186 == i + 1 || 223 == i + 1 || 224 == i + 1 || 240 == i + 1
                        || 249 == i + 1 || 250 == i + 1 || 267 == i + 1 || 268 == i + 1 || 304 == i + 1 || 343 == i + 1 || 356 == i + 1 || 395 == i + 1
                        || 419 == i + 1 || 483 == i + 1 || 558 == i + 1 || 564 == i + 1)
                    fdo++;
                if (6 == i + 1 || 28 == i + 1 || 30 == i + 1 || 33 == i + 1 || 56 == i + 1 || 116 == i + 1 || 118 == i + 1 || 157 == i + 1 || 175 == i + 1
                        || 181 == i + 1 || 223 == i + 1 || 224 == i + 1 || 260 == i + 1 || 304 == i + 1 || 419 == i + 1 || 434 == i + 1 || 437 == i + 1
                        || 468 == i + 1 || 469 == i + 1 || 471 == i + 1 || 472 == i + 1 || 529 == i + 1 || 553 == i + 1 || 558 == i + 1)
                    fre++;
                if (58 == i + 1 || 80 == i + 1 || 92 == i + 1 || 96 == i + 1 || 111 == i + 1 || 167 == i + 1 || 174 == i + 1 || 220 == i + 1 || 242 == i + 1
                        || 249 == i + 1 || 250 == i + 1 || 291 == i + 1 || 313 == i + 1 || 360 == i + 1 || 378 == i + 1 || 439 == i + 1 || 444 == i + 1
                        || 483 == i + 1 || 488 == i + 1 || 489 == i + 1 || 548 == i + 1)
                    fcn++;
            }
        }
        int k05 = 0, k04 = 0, k02 = 0;
        switch (fk) {
            case 0:
                k05 = 0;
                k04 = 0;
                k02 = 0;
                break;
            case 1:
                k05 = 1;
                k04 = 1;
                k02 = 0;
                break;
            case 2:
                k05 = 1;
                k04 = 1;
                k02 = 0;
                break;
            case 3:
                k05 = 2;
                k04 = 2;
                k02 = 1;
                break;
            case 4:
                k05 = 2;
                k04 = 2;
                k02 = 1;
                break;
            case 5:
                k05 = 3;
                k04 = 2;
                k02 = 1;
                break;
            case 6:
                k05 = 3;
                k04 = 2;
                k02 = 1;
                break;
            case 7:
                k05 = 4;
                k04 = 3;
                k02 = 1;
                break;
            case 8:
                k05 = 4;
                k04 = 3;
                k02 = 2;
                break;
            case 9:
                k05 = 5;
                k04 = 4;
                k02 = 2;
                break;
            case 10:
                k05 = 5;
                k04 = 4;
                k02 = 2;
                break;
            case 11:
                k05 = 6;
                k04 = 4;
                k02 = 2;
                break;
            case 12:
                k05 = 6;
                k04 = 5;
                k02 = 2;
                break;
            case 13:
                k05 = 7;
                k04 = 5;
                k02 = 3;
                break;
            case 14:
                k05 = 7;
                k04 = 6;
                k02 = 3;
                break;
            case 15:
                k05 = 8;
                k04 = 6;
                k02 = 3;
                break;
            case 16:
                k05 = 8;
                k04 = 6;
                k02 = 3;
                break;
            case 17:
                k05 = 9;
                k04 = 7;
                k02 = 3;
                break;
            case 18:
                k05 = 9;
                k04 = 7;
                k02 = 4;
                break;
            case 19:
                k05 = 10;
                k04 = 8;
                k02 = 4;
                break;
            case 20:
                k05 = 10;
                k04 = 8;
                k02 = 4;
                break;
            case 21:
                k05 = 11;
                k04 = 8;
                k02 = 4;
                break;
            case 22:
                k05 = 11;
                k04 = 9;
                k02 = 4;
                break;
            case 23:
                k05 = 12;
                k04 = 9;
                k02 = 5;
                break;
            case 24:
                k05 = 12;
                k04 = 10;
                k02 = 5;
                break;
            case 25:
                k05 = 13;
                k04 = 10;
                k02 = 5;
                break;
            case 26:
                k05 = 13;
                k04 = 10;
                k02 = 5;
                break;
            case 27:
                k05 = 14;
                k04 = 11;
                k02 = 5;
                break;
            case 28:
                k05 = 14;
                k04 = 11;
                k02 = 6;
                break;
            case 29:
                k05 = 15;
                k04 = 12;
                k02 = 6;
                break;
            case 30:
                k05 = 15;
                k04 = 12;
                k02 = 6;
                break;
            default:
                break;
        }
        fhs = fhs + k05;
        fpd = fpd + k04;
        fpt = fpt + fk;
        fsc = fsc + fk;
        fma = fma + k02;

        float fq_score = 0;
        float fl_score = 0;
        float ff_score = 0;
        float fk_score = 0;
        float fhs_score = 0;
        float fd_score = 0;
        float fhy_score = 0;
        float fpd_score = 0;
        float fmfm_score = 0;
        float fmff_score = 0;
        float fpa_score = 0;
        float fpt_score = 0;
        float fsc_score = 0;
        float fma_score = 0;
        float fsi_score = 0;
        float fmas_score = 0;
        float fdy_score = 0;
        float fdo_score = 0;
        float fre_score = 0;
        float fcn_score = 0;

        fq_score = 41 + fq * (float) 0.3;
        fl_score = 36 + fl * (float) 3.3333;
        ff_score = 29 + ff * (float) 1.4643;
        fk_score = 27 + fk * (float) 1.8667;
        fpd_score = 20 + (fpd - 6) * (float) 2.3571;
        fpa_score = 27 + fpa * (float) 2.9063;
        fma_score = 21 + (fma - 5) * (float) 2.175;
        fsi_score = 70 + (fsi - 1) * (float) 1.0435;

        if (gender == 0) {
            fhs_score = 21 + fhs * (float) 2.5526;
            fd_score = 29 + (fd - 8) * (float) 2.3947;
            fhy_score = 35 + (fhy - 8) * (float) 1.8043;
            fmfm_score = 26 + (fmfm - 8) * (float) 1.9535;
            fmff_score = 26 + (fmff - 8) * (float) 1.9535;
            fpt_score = 21 + (fpt - 9) * (float) 2.0625;
            fsc_score = 21 + (fsc - 7) * (float) 1.8039;
            fmas_score = 34 + fmas * (float) 1.5;
            fdy_score = 20 + (fdy - 2) * (float) 1.225;
            fdo_score = 25 + (fdo - 7) * (float) 3;
            fre_score = 29 + (fre - 9) * (float) 2.05;
            fcn_score = 33 + (fcn - 14) * (float) 1.8947;
        } else {
            fhs_score = 23 + fhs * (float) 2.0465;
            fd_score = 28 + (fd - 8) * (float) 1.9348;
            fhy_score = 24 + (fhy - 4) * (float) 1.76;
            fmfm_score = 20 + (51 - fmfm) * (float) 2.0833;
            fmff_score = 20 + (51 - fmff) * (float) 2.0833;
            fpt_score = 20 + (fpt - 7) * (float) 1.6415;
            fsc_score = 23 + (fsc - 5) * (float) 1.5323;
            fmas_score = 22 + fmas * (float) 1.3429;
            fdy_score = 20 + (fdy - 7) * (float) 1.3158;
            fdo_score = 30 + (fdo - 8) * (float) 3.0833;
            fre_score = 29 + (fre - 13) * (float) 2.6667;
            fcn_score = 32 + (fcn - 14) * (float) 2;
        }

        String fq_level = "";
        String fl_level = "";
        String ff_level = "";
        String fk_level = "";
        String fhs_level = "";
        String fd_level = "";
        String fhy_level = "";
        String fpd_level = "";
        String fmfm_level = "";
        String fmff_level = "";
        String fpa_level = "";
        String fpt_level = "";
        String fsc_level = "";
        String fma_level = "";
        String fsi_level = "";
        String fmas_level = "";
        String fdy_level = "";
        String fdo_level = "";
        String fre_level = "";
        String fcn_level = "";
        String output = "";

        if (fq_score > 50) {
            fq_level = "高分";
        } else {
            fq_level = "低分";
        }
        if (fl_score > 70) {
            fl_level = "高分";
        } else {
            fl_level = "低分";
        }
        if (ff_score > 70) {
            ff_level = "高分";
        } else {
            ff_level = "低分";
        }
        if (fk_score > 70) {
            fk_level = "高分";
        } else if (fk_score > 55) {
            fk_level = "正常";
        } else {
            fk_level = "低分";
        }

        if (fhs_score > 70) {
            fhs_level = "高分";
        } else if (fhs_score > 45) {
            fhs_level = "正常";
        } else {
            fd_level = "低分";
        }
        if (fd_score > 70) {
            fd_level = "高分";
        } else if (fd_score > 55) {
            fd_level = "正常";
        } else {
            fd_level = "低分";
        }
        if (fhy_score > 70) {
            fhy_level = "高分";
        } else if (fhy_score > 55) {
            fhy_level = "正常";
        } else {
            fhy_level = "低分";
        }
        if (fpd_score > 70) {
            fpd_level = "高分";
        } else if (fpd_score > 55) {
            fpd_level = "正常";
        } else {
            fpd_level = "低分";
        }
        if (fmfm_score > 70) {
            fmfm_level = "高分";
        } else if (fmfm_score > 55) {
            fmfm_level = "正常";
        } else {
            fmfm_level = "低分";
        }
        if (fmff_score > 70) {
            fmff_level = "高分";
        } else if (fmff_score > 55) {
            fmff_level = "正常";
        } else {
            fmff_level = "低分";
        }
        if (fpa_score > 70) {
            fpa_level = "高分";
        } else if (fpa_score > 55) {
            fpa_level = "正常";
        } else {
            fpa_level = "低分";
        }
        if (fpt_score > 70) {
            fpt_level = "高分";
        } else if (fpt_score > 55) {
            fpt_level = "正常";
        } else {
            fpt_level = "低分";
        }
        if (fsc_score > 70) {
            fsc_level = "高分";
        } else if (fsc_score > 55) {
            fsc_level = "正常";
        } else {
            fsc_level = "低分";
        }
        if (fma_score > 70) {
            fma_level = "高分";
        } else if (fma_score > 55) {
            fma_level = "正常";
        } else {
            fma_level = "低分";
        }
        if (fsi_score > 70) {
            fsi_level = "高分";
        } else if (fsi_score > 55) {
            fsi_level = "正常";
        } else {
            fsi_level = "低分";
        }
        if (fmas_score > 70) {
            fmas_level = "高分";
        } else if (fmas_score > 55) {
            fmas_level = "正常";
        } else {
            fmas_level = "低分";
        }
        if (fdy_score > 70) {
            fdy_level = "高分";
        } else if (fdy_score > 55) {
            fdy_level = "正常";
        } else {
            fdy_level = "低分";
        }
        if (fdo_score > 70) {
            fdo_level = "高分";
        } else if (fdo_score > 55) {
            fdo_level = "正常";
        } else {
            fdo_level = "低分";
        }
        if (fre_score > 70) {
            fre_level = "高分";
        } else if (fre_score > 55) {
            fre_level = "正常";
        } else {
            fre_level = "低分";
        }
        if (fcn_score > 70) {
            fcn_level = "高分";
        } else if (fcn_score > 55) {
            fcn_level = "正常";
        } else {
            fcn_level = "低分";
        }

        DtrdEntEvaluationRdMmpi mmpi = new DtrdEntEvaluationRdMmpi();
        mmpi.setInput(joiner.toString());
        mmpi.setGender(gender);
        mmpi.setFqScore(fq_score);
        mmpi.setFqLevel(fq_level);
        mmpi.setFlScore(fl_score);
        mmpi.setFlLevel(fl_level);
        mmpi.setFfScore(ff_score);
        mmpi.setFfLevel(ff_level);
        mmpi.setFkScore(fk_score);
        mmpi.setFkLevel(fk_level);
        mmpi.setFhsScore(fhs_score);
        mmpi.setFhsLevel(fhs_level);
        mmpi.setFdScore(fd_score);
        mmpi.setFdLevel(fd_level);
        mmpi.setFhyScore(fhy_score);
        mmpi.setFhyLevel(fhy_level);
        mmpi.setFpdScore(fpd_score);
        mmpi.setFpdLevel(fpd_level);
        mmpi.setFmfmScore(fmfm_score);
        mmpi.setFmfmLevel(fmfm_level);
        mmpi.setFmffScore(fmff_score);
        mmpi.setFmffLevel(fmff_level);
        mmpi.setFpaScore(fpa_score);
        mmpi.setFpaLevel(fpa_level);
        mmpi.setFptScore(fpt_score);
        mmpi.setFptLevel(fpt_level);
        mmpi.setFscScore(fsc_score);
        mmpi.setFscLevel(fsc_level);
        mmpi.setFmaScore(fma_score);
        mmpi.setFmaLevel(fma_level);
        mmpi.setFsiScore(fsi_score);
        mmpi.setFsiLevel(fsi_level);
        mmpi.setFmasScore(fmas_score);
        mmpi.setFmasLevel(fmas_level);
        mmpi.setFdyScore(fdy_score);
        mmpi.setFdyLevel(fdy_level);
        mmpi.setFdoScore(fdo_score);
        mmpi.setFdoLevel(fdo_level);
        mmpi.setFreScore(fre_score);
        mmpi.setFreLevel(fre_level);
        mmpi.setFcnScore(fcn_score);
        mmpi.setFcnLevel(fcn_level);
        mmpi.setOutput(output);
        if (mmpi.getPatientId() != null) {
            mmpi.setPatientId(dto.getPatientId());
            save(mmpi);
        }
        return new MmpiDetail().parseFromPo(mmpi);
    }

    @Override
    public boolean removeMmpi(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntEvaluationRdMmpi> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntEvaluationRdMmpi.class)
                        .eq(DtrdEntEvaluationRdMmpi::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntEvaluationRdMmpi> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntEvaluationRdMmpi.class)
                        .eq(DtrdEntEvaluationRdMmpi::getIsDel, 0);
    }
}
